<?php

/**
 * This is the model class for table "{{users}}".
 *
 * The followings are the available columns in table '{{users}}':
 * @property integer $user_id
 * @property string $user_fname
 * @property string $user_lname
 * @property string $user_email
 * @property string $user_registered
 * @property integer $user_status
 * @property string $user_group
 * @property string $user_phone
 * @property string $user_password
 * @property string $user_role
 */
class Users extends CActiveRecord {

    public $confirm_password;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{users}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that required
        // will receive user inputs.
        return array(
            array('user_fname, user_lname, user_email, user_group, user_phone, user_password, confirm_password, promo_code', 'required', 'on' => 'create'),
            array('user_fname, user_lname, user_email, user_group, user_phone', 'required', 'on' => 'update'),
            array('user_email', 'unique'),
            array('user_password', 'compare', 'compareAttribute' => 'confirm_password'),
            array('user_status', 'numerical', 'integerOnly' => true),
            array('user_fname, user_lname, user_email, user_registered, user_group, user_phone, user_password, user_role', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('user_id, user_fname, user_lname, user_email, user_registered, user_status, user_group, user_phone, user_password, user_role', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'code' => array(self::BELONGS_TO, 'Promocodes', 'promo_code'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'user_id' => 'User',
            'user_fname' => 'Имя',
            'user_lname' => 'Фамилия',
            'user_email' => 'Email',
            'user_registered' => 'User Registered',
            'user_status' => 'User Status',
            'user_group' => 'User Group',
            'user_phone' => 'Телефон',
            'user_password' => 'Пароль',
            'confirm_password' => 'Подтвердите пароль',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('user_id', $this->user_id);
        $criteria->compare('user_fname', $this->user_fname, true);
        $criteria->compare('user_lname', $this->user_lname, true);
        $criteria->compare('user_email', $this->user_email, true);
        $criteria->compare('user_registered', $this->user_registered, true);
        $criteria->compare('user_status', $this->user_status);
        $criteria->compare('user_group', $this->user_group, true);
        $criteria->compare('user_phone', $this->user_phone, true);
        $criteria->compare('user_password', $this->user_password, true);
        $criteria->compare('user_role', $this->user_role, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Users the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord)
                $this->user_registered = time();
            return true;
        }
        return FALSE;
    }

}
